package com.fivetalks.client;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fivetalks.object.Member;
import com.fivetalks.object.RoomInfo;

public class CustomAdapter extends ArrayAdapter<RoomInfo> implements
		OnClickListener {

	private Context mContext;
	private int mResource;
	private ArrayList<RoomInfo> mList;
	private LayoutInflater mInflater;
	private Member myself;

	
	public CustomAdapter(Context context, int layoutResource,
			ArrayList<RoomInfo> objects, Member myself) {
		super(context, layoutResource, objects);
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.myself = myself;
	}

	public CustomAdapter(Context context, int layoutResource,
			ArrayList<RoomInfo> objects) {
		super(context, layoutResource, objects);
		this.mContext = context;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		RoomInfo room = mList.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(mResource, null);
		}

		if (room != null) {

			TextView tvCurrPeople = (TextView) convertView
					.findViewById(R.id.tvCurrPeople);
			TextView tvTitle = (TextView) convertView
					.findViewById(R.id.tvTitle);
			ImageView ivStatus = (ImageView) convertView
					.findViewById(R.id.ivStatus);
		//	TextView tvStatus = (TextView) convertView
		//			.findViewById(R.id.tvStatus);
			
			tvCurrPeople.setTextColor(Color.WHITE);
			tvTitle.setTextSize(20);
			tvTitle.setTextColor(Color.WHITE);
			if("Ready".equals(room.getStatus())) {
				ivStatus.setImageResource(R.drawable.ready_green);
			}
			else {
				ivStatus.setImageResource(R.drawable.ready_red);
			}
			
	//		tvStatus.setTextColor(Color.WHITE);
			tvCurrPeople.setText("남자 : " + room.getCurrMale() + "/"
					+ room.getLimitCount() + "\r\n" + "여자 : "
					+ room.getCurrFemale() + "/" + room.getLimitCount());
			tvTitle.setText(room.getTitle());
			Log.d(room.getStatus(), "이다");
		}

		convertView.setTag(position);
		convertView.setOnClickListener(this);

		return convertView;

	}

	public RoomInfo getItem(int position) {
		return mList.get(position);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int position = (Integer) v.getTag();

		RoomInfo room = getItem(position);

		if ("MALE".equals(myself.getGender())) {
			if (room.getCurrMale() + 1 <= room.getLimitCount()) {
				Intent intent = new Intent(mContext, PreChatActivity.class);
				intent.putExtra("roomNumber", room.getRoomNumber());
				mContext.startActivity(intent);
			} else
				Toast.makeText(mContext, "Full", Toast.LENGTH_SHORT).show();
		} else if ("FEMALE".equals(myself.getGender())) {
			if (room.getCurrFemale() + 1 <= room.getLimitCount()) {
				Intent intent = new Intent(mContext, PreChatActivity.class);
				intent.putExtra("roomNumber", room.getRoomNumber());
				mContext.startActivity(intent);
			} else
				Toast.makeText(mContext, "Full", Toast.LENGTH_SHORT).show();
		}
	}
}
